package ex1.gui;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

/**
 * 
 * @author Assaf & Orr
 * 
 */
public class ImagePanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private BufferedImage m_Image;

	public ImagePanel() 
	{
		super();
		this.setDoubleBuffered(false);
		this.setPreferredSize(new Dimension(320,240));
	}

	public BufferedImage getImage() 
	{
		return m_Image;
	}

	/**
	 * Overrides the JPanel's paint method to draw the image
	 */
	public void paint(Graphics g) 
	{
		g.drawImage(m_Image, 0, 0, null);
	}

	/**
	 * Sets the image to be displayed. 
	 * @param image Any BufferedImage
	 */
	public void setImage(BufferedImage image) 
	{
		if(image == null)
		{
			return;
		}
		
		this.m_Image = image;
		setSize(new Dimension(image.getWidth(), image.getHeight()));
		setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
	}

	/**
	 * Paint without cleaning the buffer
	 */
	public void update(Graphics g) 
	{
		paint(g);
	}
}